package com.ynet.middleground.user.model;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ynet.core.common.BaseReqObj;
import com.ynet.middleground.user.bean.*;
import com.ynet.middleground.user.common.SystemConstant;
import com.ynet.middleground.user.dao.*;
import com.ynet.middleground.user.entity.*;
import com.ynet.middleground.user.external.cloudPlatform.CloudPlatformConfig;
import com.ynet.middleground.user.mq.producer.ZtProducer;
import com.ynet.middleground.user.utils.CloudPlatformUtil;
import com.ynet.middleground.user.utils.GeneralService;

/**
 * @author liwq
 * @description 云平台异步处理
 * @date 2022-01-25 16:39
 */
@Component
public class CloudPlatformAsync {
    @Autowired
    private EnterpriseUserInfoMapper enterpriseUserInfoMapper;

    @Autowired
    private NaturalPersonBaseInformationMapper naturalPersonBaseInformationMapper;

    @Autowired
    private EnterpriseBasicInfoMapper enterpriseBasicInfoMapper;

    @Autowired
    private UserBaseInformationMapper userBaseInformationMapper;

    @Autowired
    GeneralService generalService;

    @Autowired
    CloudPlatformSyncMapper cloudPlatformSyncMapper;

    @Autowired
    private CloudPlatformConfig cloudPlatformConfig;

    @Autowired
    private CloudPlatformUtil cloudPlatformUtil;

    @Autowired
    private CloudPlatformIdRelationMapper cloudPlatformIdRelationMapper;

    @Autowired
    private CloudPlatformRegionRelationMapper cloudPlatformRegionRelationMapper;

    /**
     * 对接云平台；将企业员工数据 同步到云平台
     *
     * @param requestObject
     */
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void mergeDataToCloudPlatform(AddEnterpriseUserInfoReq requestObject, Integer middleGroundUserId) {
        try {
            // step1: 判断上传渠道是否在对接列表之内
            String channelList = cloudPlatformConfig.getChannelList();
            if (StringUtils.isEmpty(channelList) || !channelList.contains(requestObject.getChannel())) {
                return;
            }

            // step2: 获取token
            String md5Token = cloudPlatformUtil.getMd5Token(requestObject);

            // step3: 根据手机号判断此员工在云平台是否存在
            String userId = null;
            String userInfoId = null;
            String userInfo = null;
            userInfo = cloudPlatformUtil.getUserInfo(requestObject, requestObject.getTelephone(), md5Token);

            // step4: 新增||修改||关系入库
            CloudPlatformUserReq c = null;
            if (StringUtils.isEmpty(userInfo)) {
                // 在云平台不存在，先新增，然后认证
                // 新增
                c = new CloudPlatformUserReq();
                c.setUserTel(requestObject.getTelephone());
                c.setEmail(requestObject.getEmail());
                c.setOperateFlag("0");
                c.setMiddleGroundUserId(middleGroundUserId);
                userInfo = cloudPlatformUtil.commitUser(c, md5Token, requestObject, null);
                // 新增成功后，认证
                if (!StringUtils.isEmpty(userInfo)) {
                    // 取出userId 和 userInfoId
                    userId = userInfo.substring(0, userInfo.indexOf("+"));
                    userInfoId = userInfo.substring(userInfo.indexOf("+") + 1);
                    c = new CloudPlatformUserReq();
                    c.setUserId(userInfoId);
                    c.setUserName(requestObject.getUserName());
                    c.setRealName(requestObject.getUserName());
                    c.setUserCardNo(requestObject.getPapersNo());
                    c.setIsAuth(1);
                    c.setOperateFlag("1");
                    c.setMiddleGroundUserId(middleGroundUserId);
                    cloudPlatformUtil.commitUser(c, md5Token, requestObject, null);
                }
            } else {
                // 在云平台存在，修改
                // 取出userId 和 userInfoId
                userId = userInfo.substring(0, userInfo.indexOf("+"));
                userInfoId = userInfo.substring(userInfo.indexOf("+") + 1);
                c = new CloudPlatformUserReq();
                c.setUserId(userInfoId);
                c.setUserName(requestObject.getUserName());
                c.setRealName(requestObject.getUserName());
                c.setUserCardNo(requestObject.getPapersNo());
                c.setUserTel(requestObject.getTelephone());
                c.setEmail(requestObject.getEmail());
                c.setOperateFlag("2");
                c.setMiddleGroundUserId(middleGroundUserId);
                cloudPlatformUtil.commitUser(c, md5Token, requestObject, null);
            }

            // 将userId对应关系入库
            if (!StringUtils.isEmpty(userId)) {
                LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(CloudPlatformIdRelation::getUserId, middleGroundUserId);
                queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, userId);
                queryWrapper.eq(CloudPlatformIdRelation::getEnterpriseId, requestObject.getEnterpriseId());
                queryWrapper.eq(CloudPlatformIdRelation::getChannel, requestObject.getChannel());
                CloudPlatformIdRelation cloudPlatformIdRelation = cloudPlatformIdRelationMapper.selectOne(queryWrapper);
                if (cloudPlatformIdRelation == null) {
                    // 将userId对应关系入库
                    CloudPlatformIdRelation cr = new CloudPlatformIdRelation();
                    cr.setUserType("3");
                    cr.setChannel(requestObject.getChannel());
                    cr.setUserId(middleGroundUserId);
                    cr.setPlatformId(userId);
                    cr.setPlatformUserInfoId(userInfoId);
                    cr.setEnterpriseId(String.valueOf(requestObject.getEnterpriseId()));
                    cr.setCreateBy(requestObject.getOperationUserId());
                    cr.setCreationDate(LocalDateTime.now());
                    cr.setModifiedBy(requestObject.getOperationUserId());
                    cr.setGmtModified(LocalDateTime.now());
                    cloudPlatformIdRelationMapper.insert(cr);
                }
            }
        } catch (Exception e) {
            // 将异常数据记录到错误表
            cloudPlatformUtil.addErrorData(requestObject.getChannel(), middleGroundUserId, "3",
                requestObject.getOperationUserId(), e.toString(), e.toString());
            e.printStackTrace();
        }
    }

    // 将企业员工和自然人信息同步到云平台
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void mergeUserDataToCloudPlatform(ChangeUserInfoReq requestObject, String oldPhone, String oldPapersNo) {
        try {
            // step1: 判断上传渠道是否在对接列表之内
            String channelList = cloudPlatformConfig.getChannelList();
            if (StringUtils.isEmpty(channelList) || !channelList.contains(requestObject.getChannel())) {
                return;
            }

            // step2: 获取token
            String md5Token = cloudPlatformUtil.getMd5Token(requestObject);

            // step3: 判断修改的是企业员工，还是自然人。
            LambdaQueryWrapper<UserBaseInformation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserBaseInformation::getUserId, requestObject.getUserId());
            queryWrapper.eq(UserBaseInformation::getIsDelete, 0);
            queryWrapper.eq(UserBaseInformation::getChannel, requestObject.getChannel());
            UserBaseInformation userBaseInformation = userBaseInformationMapper.selectOne(queryWrapper);
            if (userBaseInformation == null) {
                return;
            }

            // step3.1: 企业员工
            String userInfo = null;
            if ("3".equals(userBaseInformation.getUserType())) {
                // 因为注册企业员工阶段，手机号必填，所以手机号一般有值，若没有值，则认为脏数据，本次不操作。
                if (StringUtils.isEmpty(oldPhone)) {
                    return;
                }
                // step3.1.1: 新老手机号相同，或者新手机号为空，代表本次不修改手机号。那么就操作老手机号对应的那条数据。
                if (StringUtils.isEmpty(requestObject.getTelephone())
                    || oldPhone.equals(requestObject.getTelephone())) {
                    CloudPlatformUserReq c = null;
                    // 根据老手机号拉取，看是否在云平台存在,由此判断新增|修改
                    userInfo = cloudPlatformUtil.getUserInfo(requestObject, oldPhone, md5Token);
                    requestObject.setTelephone(oldPhone);
                    addOrUpdateUser(requestObject, userInfo, md5Token);
                    return;
                }
                // step3.1.2: 新老不同
                if (!oldPhone.equals(requestObject.getTelephone())) {
                    String userInfoOld = cloudPlatformUtil.getUserInfo(requestObject, oldPhone, md5Token);
                    String userInfoNew =
                        cloudPlatformUtil.getUserInfo(requestObject, requestObject.getTelephone(), md5Token);
                    // 1 老手机号存在，新手机号不存在
                    if (StringUtils.isNotEmpty(userInfoOld) && StringUtils.isEmpty(userInfoNew)) {
                        // 修改老手机号对应的那条数据
                        addOrUpdateUser(requestObject, userInfoOld, md5Token);
                        return;
                    }
                    // 2 老手机号存在，新手机号也存在
                    if (StringUtils.isNotEmpty(userInfoOld) && StringUtils.isNotEmpty(userInfoNew)) {
                        // 修改新手机号对应的那条数据
                        addOrUpdateUser(requestObject, userInfoNew, md5Token);
                        return;
                    }
                    // 3 老手机号不存在，新手机号不存在
                    if (StringUtils.isEmpty(userInfoOld) && StringUtils.isEmpty(userInfoNew)) {
                        // 调用云平台“用户数据保存-新增”接口，新增数据。然后再调用云平台“用户实名认证”接口，完善数据。然后将该用户在中台的Id和在云平台的Id维护到中台数据库。
                        addOrUpdateUser(requestObject, userInfoNew, md5Token);
                        return;
                    }
                    // 4 老手机号不存在，新手机号存在
                    if (StringUtils.isEmpty(userInfoOld) && StringUtils.isNotEmpty(userInfoNew)) {
                        // 修改新手机号对应的那条数据
                        addOrUpdateUser(requestObject, userInfoNew, md5Token);
                        return;
                    }
                }

            }
            // step3.2: 自然人
            String personId = null;
            if ("2".equals(userBaseInformation.getUserType())) {
                String newPapersNo = requestObject.getPapersNo();
                // step3.2.1:若新证件号、老证件号都为空，本次不操作。
                if (StringUtils.isEmpty(newPapersNo) && StringUtils.isEmpty(oldPapersNo)) {
                    return;
                }
                // step3.2.2: 新老证件号相同，或者新证件号为空，代表本次不修改证件号。那么就操作老证件号对应的那条数据。
                if (StringUtils.isEmpty(newPapersNo) || newPapersNo.equals(oldPapersNo)) {
                    personId = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                    requestObject.setPapersNo(oldPapersNo);
                    addOrUpdatePerson(requestObject, personId, md5Token);
                    return;
                }
                // step3.2.3: 新老不同
                if (!newPapersNo.equals(oldPapersNo)) {
                    String personIdOld = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                    String personIdNew = cloudPlatformUtil.getCompanyInfo(requestObject, newPapersNo, md5Token);
                    // 1 老证件号存在，新证件号不存在
                    if (StringUtils.isNotEmpty(personIdOld) && StringUtils.isEmpty(personIdNew)) {
                        // 修改老证件号对应的那条数据
                        addOrUpdatePerson(requestObject, personIdOld, md5Token);
                        return;
                    }
                    // 2 老证件号存在，新证件号也存在
                    if (StringUtils.isNotEmpty(personIdOld) && StringUtils.isNotEmpty(personIdNew)) {
                        // 修改新证件号对应的那条数据
                        addOrUpdatePerson(requestObject, personIdNew, md5Token);
                        return;
                    }
                    // 3 老证件号不存在，新证件号不存在
                    if (StringUtils.isEmpty(personIdOld) && StringUtils.isEmpty(personIdNew)) {
                        // 新增数据
                        addOrUpdatePerson(requestObject, personIdNew, md5Token);
                        return;
                    }
                    // 4 老证件号不存在，新证件号存在
                    if (StringUtils.isEmpty(personIdOld) && StringUtils.isNotEmpty(personIdNew)) {
                        // 修改新证件号对应的那条数据
                        addOrUpdatePerson(requestObject, personIdNew, md5Token);
                        return;
                    }
                }

            }
        } catch (Exception e) {
            // 将异常数据记录到错误表
            cloudPlatformUtil.addErrorData(requestObject.getChannel(), requestObject.getUserId(),
                requestObject.getChangeUserInfoEnterpriseUserInfo() != null ? "3" : "2",
                requestObject.getOperationUserId(), e.toString(), e.toString());
            e.printStackTrace();
        }
    }

    // 云平台企业员工新增||修改
    public void addOrUpdateUser(ChangeUserInfoReq requestObject, String userInfo, String md5Token) {
        CloudPlatformUserReq c = null;
        String userId = null;
        String userInfoId = null;
        if (StringUtils.isEmpty(userInfo)) {
            // 新增
            c = new CloudPlatformUserReq();
            c.setUserTel(requestObject.getTelephone());
            c.setEmail(requestObject.getChangeUserInfoEnterpriseUserInfo().getEmail());
            c.setOperateFlag("0");
            c.setMiddleGroundUserId(requestObject.getUserId());
            userInfo = cloudPlatformUtil.commitUser(c, md5Token, requestObject, null);
            // 认证
            if (!StringUtils.isEmpty(userInfo)) {
                userId = userInfo.substring(0, userInfo.indexOf("+"));
                userInfoId = userInfo.substring(userInfo.indexOf("+") + 1);
                c = new CloudPlatformUserReq();
                c.setUserId(userInfoId);
                c.setUserName(requestObject.getUserName());
                c.setRealName(requestObject.getUserName());
                c.setUserCardNo(requestObject.getPapersNo());
                c.setOperateFlag("1");
                c.setMiddleGroundUserId(requestObject.getUserId());
                cloudPlatformUtil.commitUser(c, md5Token, requestObject, null);
            }
        } else {
            // 在云平台存在，修改
            userId = userInfo.substring(0, userInfo.indexOf("+"));
            userInfoId = userInfo.substring(userInfo.indexOf("+") + 1);
            c = new CloudPlatformUserReq();
            c.setUserId(userInfoId);
            c.setUserName(requestObject.getUserName());
            c.setRealName(requestObject.getUserName());
            c.setUserCardNo(requestObject.getPapersNo());
            c.setUserTel(requestObject.getTelephone());
            c.setEmail(requestObject.getChangeUserInfoEnterpriseUserInfo().getEmail());
            c.setOperateFlag("2");
            c.setMiddleGroundUserId(requestObject.getUserId());
            cloudPlatformUtil.commitUser(c, md5Token, requestObject, null);
        }

        // 将userId对应关系入库
        if (!StringUtils.isEmpty(userId)) {
            LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CloudPlatformIdRelation::getUserId, requestObject.getUserId());
            queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, userId);
            queryWrapper.eq(CloudPlatformIdRelation::getEnterpriseId, requestObject.getEnterpriseId());
            queryWrapper.eq(CloudPlatformIdRelation::getChannel, requestObject.getChannel());
            CloudPlatformIdRelation cloudPlatformIdRelation = cloudPlatformIdRelationMapper.selectOne(queryWrapper);
            if (cloudPlatformIdRelation == null) {
                CloudPlatformIdRelation cr = new CloudPlatformIdRelation();
                cr.setUserType("3");
                cr.setChannel(requestObject.getChannel());
                cr.setUserId(requestObject.getUserId());
                cr.setPlatformId(userId);
                cr.setPlatformUserInfoId(userInfoId);
                cr.setEnterpriseId(String.valueOf(requestObject.getEnterpriseId()));
                cr.setCreateBy(requestObject.getOperationUserId());
                cr.setCreationDate(LocalDateTime.now());
                cr.setModifiedBy(requestObject.getOperationUserId());
                cr.setGmtModified(LocalDateTime.now());
                c.setEmail(requestObject.getChangeUserInfoEnterpriseUserInfo().getEmail());
                cloudPlatformIdRelationMapper.insert(cr);
            }
        }
    }

    // 云平台客商（自然人）新增||修改
    public void addOrUpdatePerson(ChangeUserInfoReq requestObject, String personId, String md5Token) {
        CloudPlatformMerchantReq c = null;
        // 在云平台不存在，新增
        if (StringUtils.isEmpty(personId)) {
            // 新增
            c = new CloudPlatformMerchantReq();
            c.setCustomerType(0);
            c.setCompanyName(requestObject.getUserName());
            c.setUniformSocialCreditCode(requestObject.getPapersNo());
            c.setCompanyTel(requestObject.getTelephone());
            c.setOperateFlag("0");
            c.setMiddleGroundUserId(requestObject.getUserId());
            personId = cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, null);
        } else {
            // 在云平台存在，修改
            c = new CloudPlatformMerchantReq();
            c.setCompanyId(personId);
            c.setCustomerType(0);
            c.setCompanyName(requestObject.getUserName());
            c.setUniformSocialCreditCode(requestObject.getPapersNo());
            c.setCompanyTel(requestObject.getTelephone());
            c.setOperateFlag("1");
            c.setMiddleGroundUserId(requestObject.getUserId());
            cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, null);
        }

        // 将userId对应关系入库
        if (!StringUtils.isEmpty(personId)) {
            LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CloudPlatformIdRelation::getUserId, requestObject.getUserId());
            queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, personId);
            queryWrapper.eq(CloudPlatformIdRelation::getChannel, requestObject.getChannel());
            CloudPlatformIdRelation cloudPlatformIdRelation = cloudPlatformIdRelationMapper.selectOne(queryWrapper);
            if (cloudPlatformIdRelation == null) {
                CloudPlatformIdRelation cr = new CloudPlatformIdRelation();
                cr.setUserType("2");
                cr.setChannel(requestObject.getChannel());
                cr.setUserId(requestObject.getUserId());
                cr.setPlatformId(personId);
                cr.setEnterpriseId(
                    requestObject.getEnterpriseId() == null ? null : String.valueOf(requestObject.getEnterpriseId()));
                cr.setCreateBy(requestObject.getOperationUserId());
                cr.setCreationDate(LocalDateTime.now());
                cr.setModifiedBy(requestObject.getOperationUserId());
                cr.setGmtModified(LocalDateTime.now());
                cloudPlatformIdRelationMapper.insert(cr);
            }
        }
    }

    /**
     * 将企业客户、自然人客户同步到云平台
     *
     * @param requestObject
     */
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void mergeDataToCloudPlatform(UserRegistrationReq requestObject, Integer middleGroundUserId) {
        try {
            // step1: 判断上传渠道是否在对接列表之内
            String channelList = cloudPlatformConfig.getChannelList();
            if (StringUtils.isEmpty(channelList) || !channelList.contains(requestObject.getChannel())) {
                return;
            }
            // 若证件号为空，本次不操作
            if (StringUtils.isEmpty(requestObject.getPapersNo())) {
                return;
            }

            // step2: 获取token
            String md5Token = cloudPlatformUtil.getMd5Token(requestObject);

            // step3: 判断注册的是企业客户还是自然人
            // 0-企业用户
            String companyId = null;
            if ("0".equals(requestObject.getUserType())) {
                // 根据上送的“证件编号（统一社会信用代码）”调用云平台“客商数据拉取”接口，查看该企业数据在云平台是否存在
                companyId = cloudPlatformUtil.getCompanyInfo(requestObject, requestObject.getPapersNo(), md5Token);
                addOrUpdateCompany(requestObject, companyId, md5Token, middleGroundUserId);
                return;
            }
            // 2-个人用户
            String personId = null;
            if ("2".equals(requestObject.getUserType())) {
                // 根据上送的“证件编号（统一社会信用代码）”调用云平台“客商数据拉取”接口，查看该个人数据在云平台是否存在
                personId = cloudPlatformUtil.getCompanyInfo(requestObject, requestObject.getPapersNo(), md5Token);
                addOrUpdatePerson(requestObject, personId, md5Token, middleGroundUserId);
                return;
            }
        } catch (Exception e) {
            // 将异常数据记录到错误表
            cloudPlatformUtil.addErrorData(requestObject.getChannel(), middleGroundUserId, requestObject.getUserType(),
                requestObject.getOperationUserId(), e.toString(), e.toString());
            e.printStackTrace();
        }
    }

    // 云平台客商（自然人）新增||修改
    public void addOrUpdatePerson(UserRegistrationReq requestObject, String personId, String md5Token,
        Integer middleGroundUserId) {
        CloudPlatformMerchantReq c = null;
        // 在云平台不存在，新增
        if (StringUtils.isEmpty(personId)) {
            // 新增
            c = new CloudPlatformMerchantReq();
            c.setCustomerType(0);
            c.setCompanyName(requestObject.getUserName());
            c.setUniformSocialCreditCode(requestObject.getPapersNo());
            c.setCompanyTel(requestObject.getTelephone());
            c.setOperateFlag("0");
            c.setMiddleGroundUserId(middleGroundUserId);
            personId = cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, null);
        } else {
            // 在云平台存在，修改
            c = new CloudPlatformMerchantReq();
            c.setCompanyId(personId);
            c.setCustomerType(0);
            c.setCompanyName(requestObject.getUserName());
            c.setUniformSocialCreditCode(requestObject.getPapersNo());
            c.setCompanyTel(requestObject.getTelephone());
            c.setOperateFlag("1");
            c.setMiddleGroundUserId(middleGroundUserId);
            cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, null);
        }

        // 将userId对应关系入库
        if (!StringUtils.isEmpty(personId)) {
            LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CloudPlatformIdRelation::getUserId, middleGroundUserId);
            queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, personId);
            queryWrapper.eq(CloudPlatformIdRelation::getChannel, requestObject.getChannel());
            CloudPlatformIdRelation cloudPlatformIdRelation = cloudPlatformIdRelationMapper.selectOne(queryWrapper);
            if (cloudPlatformIdRelation == null) {
                // 将userId对应关系入库
                CloudPlatformIdRelation cr = new CloudPlatformIdRelation();
                cr.setUserType("2");
                cr.setChannel(requestObject.getChannel());
                cr.setUserId(middleGroundUserId);
                cr.setPlatformId(personId);
                cr.setCreateBy(requestObject.getOperationUserId());
                cr.setCreationDate(LocalDateTime.now());
                cr.setModifiedBy(requestObject.getOperationUserId());
                cr.setGmtModified(LocalDateTime.now());
                cloudPlatformIdRelationMapper.insert(cr);
            }
        }
    }

    // 云平台客商（企业）新增||修改
    public void addOrUpdateCompany(UserRegistrationReq requestObject, String companyId, String md5Token,
        Integer middleGroundUserId) {
        CloudPlatformMerchantReq c = null;
        // 在云平台不存在，新增
        if (StringUtils.isEmpty(companyId)) {
            // 新增
            c = new CloudPlatformMerchantReq();
            c.setOperateFlag("0");
            c.setCustomerType(1);
            c.setCompanyName(requestObject.getCompanyName());
            c.setUniformSocialCreditCode(requestObject.getPapersNo());
            c.setCompanyTel(requestObject.getTelephone());
            c.setMiddleGroundUserId(middleGroundUserId);
            companyId = cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, null);
        } else {
            // 在云平台存在，修改
            c = new CloudPlatformMerchantReq();
            c.setOperateFlag("1");
            c.setCustomerType(1);
            c.setCompanyId(companyId);
            c.setCompanyName(requestObject.getCompanyName());
            c.setUniformSocialCreditCode(requestObject.getPapersNo());
            c.setCompanyTel(requestObject.getTelephone());
            c.setMiddleGroundUserId(middleGroundUserId);
            cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, null);
        }

        // 将userId对应关系入库
        if (!StringUtils.isEmpty(companyId)) {
            LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CloudPlatformIdRelation::getUserId, middleGroundUserId);
            queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, companyId);
            queryWrapper.eq(CloudPlatformIdRelation::getChannel, requestObject.getChannel());
            CloudPlatformIdRelation cloudPlatformIdRelation = cloudPlatformIdRelationMapper.selectOne(queryWrapper);
            if (cloudPlatformIdRelation == null) {
                // 将userId对应关系入库
                CloudPlatformIdRelation cr = new CloudPlatformIdRelation();
                cr.setUserType("0");
                cr.setChannel(requestObject.getChannel());
                cr.setUserId(middleGroundUserId);
                cr.setPlatformId(companyId);
                cr.setCreateBy(requestObject.getOperationUserId());
                cr.setCreationDate(LocalDateTime.now());
                cr.setModifiedBy(requestObject.getOperationUserId());
                cr.setGmtModified(LocalDateTime.now());
                cloudPlatformIdRelationMapper.insert(cr);
            }
        }
    }

    /**
     * 对接云平台；认证将企业数据 同步到云平台
     *
     * @param requestObject
     */
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void authenticationToCloudPlatformCheck(BaseReqObj requestObject, String userType, String oldPapersNo,
        String newPapersNo, Integer userId) {
        try {
            // step1: 判断上传渠道是否在对接列表之内
            String channelList = cloudPlatformConfig.getChannelList();
            if (StringUtils.isEmpty(channelList) || !channelList.contains(requestObject.getChannel())) {
                return;
            }

            // step2: 获取token
            String md5Token = cloudPlatformUtil.getMd5Token(requestObject);
            if (SystemConstant.USER_TYPE_0.equals(userType)) {
                LambdaQueryWrapper<EnterpriseBasicInfo> queryEBI = new LambdaQueryWrapper<>();
                queryEBI.eq(EnterpriseBasicInfo::getUserId, userId);
                if (enterpriseBasicInfoMapper.selectCount(queryEBI) > 0) {
                    String companyId = null;
                    EnterpriseBasicInfo enterpriseBasicInfo = enterpriseBasicInfoMapper.selectList(queryEBI).get(0);
                    CloudPlatformMerchantReq c = new CloudPlatformMerchantReq();
                    c.setCustomerType(1);
                    c.setCompanyName(enterpriseBasicInfo.getCompanyName());
                    c.setIndustryType(enterpriseBasicInfo.getInternationalIndustryClass());
                    c.setMiddleGroundUserId(userId);
                    String province = null;
                    String city = null;
                    LambdaQueryWrapper<CloudPlatformRegionRelation> queryProvince = new LambdaQueryWrapper<>();
                    queryProvince.eq(CloudPlatformRegionRelation::getUserRegionCode,
                        enterpriseBasicInfo.getOpenAccountProvince());
                    if (cloudPlatformRegionRelationMapper.selectCount(queryProvince) > 0) {
                        CloudPlatformRegionRelation cloudPlatformRegionRelation =
                            cloudPlatformRegionRelationMapper.selectOne(queryProvince);
                        province = cloudPlatformRegionRelation.getPlatformRegionCode();
                        if (StringUtils.isNotBlank(province)) {
                            c.setProvince(province);
                        } else {
                            c.setProvince(cloudPlatformRegionRelation.getUserRegionName());
                        }
                    }
                    LambdaQueryWrapper<CloudPlatformRegionRelation> queryCtiy = new LambdaQueryWrapper<>();
                    queryCtiy.eq(CloudPlatformRegionRelation::getUserRegionCode,
                        enterpriseBasicInfo.getOpeningAccountCity());
                    if (cloudPlatformRegionRelationMapper.selectCount(queryCtiy) > 0) {
                        CloudPlatformRegionRelation cloudPlatformRegionRelation =
                            cloudPlatformRegionRelationMapper.selectOne(queryCtiy);
                        city = cloudPlatformRegionRelation.getPlatformRegionCode();
                        if (StringUtils.isNotBlank(city)) {
                            c.setCity(city);
                        } else {
                            c.setCity(cloudPlatformRegionRelation.getUserRegionName());
                        }
                    }
                    c.setAddress(enterpriseBasicInfo.getEntRegeditAddress());
                    if (SystemConstant.PAPERS_TYPE_01.equals(enterpriseBasicInfo.getPapersType())) {
                        c.setOrganizationCode(enterpriseBasicInfo.getPapersNo());
                    } else {
                        c.setUniformSocialCreditCode(enterpriseBasicInfo.getPapersNo());
                    }
                    c.setBusinessType(enterpriseBasicInfo.getEnterpriseType());
                    c.setLegalPerson(enterpriseBasicInfo.getLegalRepresentativeName());
                    c.setLegalPersonCardNo(enterpriseBasicInfo.getLrnCertificateNo());
                    c.setBusinessState(enterpriseBasicInfo.getBusinessConditions());
                    c.setBusinessScope(enterpriseBasicInfo.getMainBusiness());
                    c.setCompanyTel(enterpriseBasicInfo.getTelephone());
                    c.setBusinessAddress(enterpriseBasicInfo.getBusinessAddress());
                    if (StringUtils.isNotBlank(enterpriseBasicInfo.getBusinessLicenseUrl())) {
                        c.setLicenseImgUrl(
                            generalService.getOssUrl(requestObject, enterpriseBasicInfo.getBusinessLicenseUrl()));
                    }
                    if (StringUtils.isNotBlank(oldPapersNo)) {
                        // 原证件号不为空，本次修改
                        if (StringUtils.isNotBlank(newPapersNo)) {
                            // 原证件号与本次相同
                            if (newPapersNo.equals(oldPapersNo)) {
                                companyId = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                                // 在客商云平台存在
                                authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                    null);
                            } else {
                                // 原证件号与本次不同
                                companyId = cloudPlatformUtil.getCompanyInfo(requestObject, newPapersNo, md5Token);
                                if (StringUtils.isBlank(companyId)) {
                                    companyId = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                                }
                                authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                    null);
                            }
                        } else {
                            // 原证件号不为空，本次未修改
                            companyId = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                            authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                null);
                        }
                    } else {
                        // 原证件号为空，本次修改不为空
                        if (StringUtils.isNotBlank(newPapersNo)) {
                            companyId = cloudPlatformUtil.getCompanyInfo(requestObject, newPapersNo, md5Token);
                            authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                null);
                        } else {
                            // 原证件号为空，本次也为空
                        }
                    }
                }
            } else if (SystemConstant.USER_TYPE_2.equals(userType)) {
                LambdaQueryWrapper<NaturalPersonBaseInformation> queryEBI = new LambdaQueryWrapper<>();
                queryEBI.eq(NaturalPersonBaseInformation::getUserId, userId);
                if (naturalPersonBaseInformationMapper.selectCount(queryEBI) > 0) {
                    String companyId = null;
                    NaturalPersonBaseInformation naturalPersonBaseInformation =
                        naturalPersonBaseInformationMapper.selectList(queryEBI).get(0);
                    CloudPlatformMerchantReq c = new CloudPlatformMerchantReq();
                    c.setCustomerType(0);
                    c.setCompanyName(naturalPersonBaseInformation.getUserName());
                    c.setCompanyTel(naturalPersonBaseInformation.getTelephone());
                    c.setUniformSocialCreditCode(naturalPersonBaseInformation.getPapersNo());
                    c.setMiddleGroundUserId(userId);
                    if (StringUtils.isNotBlank(oldPapersNo)) {
                        // 原证件号不为空，本次修改
                        if (StringUtils.isNotBlank(newPapersNo)) {
                            // 原证件号与本次相同
                            if (newPapersNo.equals(oldPapersNo)) {
                                companyId = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                                // 在客商云平台存在
                                authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                    null);
                                return;
                            } else {
                                // 原证件号与本次不同
                                companyId = cloudPlatformUtil.getCompanyInfo(requestObject, newPapersNo, md5Token);
                                if (StringUtils.isBlank(companyId)) {
                                    companyId = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                                }
                                authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                    null);
                                return;
                            }
                        } else {
                            // 原证件号不为空，本次未修改
                            companyId = cloudPlatformUtil.getCompanyInfo(requestObject, oldPapersNo, md5Token);
                            authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                null);
                            return;
                        }
                    } else {
                        // 原证件号为空，本次修改不为空
                        if (StringUtils.isNotBlank(newPapersNo)) {
                            companyId = cloudPlatformUtil.getCompanyInfo(requestObject, newPapersNo, md5Token);
                            authenticationToCloudPlatform(requestObject, companyId, md5Token, c, userType, userId,
                                null);
                            return;
                        } else {
                            // 原证件号为空，本次也为空
                            return;
                        }
                    }
                }
            }
        } catch (Exception e) {
            // 将异常数据记录到错误表
            cloudPlatformUtil.addErrorData(requestObject.getChannel(), userId, userType,
                requestObject.getOperationUserId(), e.toString(), e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 对接云平台；将企业数据 同步到云平台
     *
     * @param requestObject
     */
    public String authenticationToCloudPlatform(BaseReqObj requestObject, String companyId, String md5Token,
        CloudPlatformMerchantReq c, String userType, Integer userId, String operation) {
        try {
            String cloudPaltformId = "";
            if (StringUtils.isNotBlank(companyId)) {
                cloudPaltformId = companyId;
                LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(CloudPlatformIdRelation::getUserId, userId);
                queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, companyId);
                if (cloudPlatformIdRelationMapper.selectCount(queryWrapper) == 0) {
                    CloudPlatformIdRelation cloudPlatformIdRelation = new CloudPlatformIdRelation();
                    cloudPlatformIdRelation.setPlatformId(companyId);
                    cloudPlatformIdRelation.setChannel(requestObject.getChannel());
                    cloudPlatformIdRelation.setUserType(userType);
                    cloudPlatformIdRelation.setUserId(userId);
                    cloudPlatformIdRelation.setCreateBy(requestObject.getOperationUserId());
                    cloudPlatformIdRelation.setCreationDate(LocalDateTime.now());
                    cloudPlatformIdRelation.setGmtModified(LocalDateTime.now());
                    cloudPlatformIdRelation.setModifiedBy(requestObject.getOperationUserId());
                    cloudPlatformIdRelationMapper.insert(cloudPlatformIdRelation);
                }
                c.setOperateFlag("1");
                c.setCompanyId(companyId);
                c.setMiddleGroundUserId(userId);
                cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, operation);
            } else {
                c.setOperateFlag("0");
                c.setMiddleGroundUserId(userId);
                String platformId = cloudPlatformUtil.commitMerchant(c, md5Token, requestObject, operation);
                cloudPaltformId = platformId;
                if (StringUtils.isNotBlank(platformId)) {
                    LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(CloudPlatformIdRelation::getUserId, userId);
                    queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, platformId);
                    if (cloudPlatformIdRelationMapper.selectCount(queryWrapper) == 0) {
                        CloudPlatformIdRelation cloudPlatformIdRelation = new CloudPlatformIdRelation();
                        cloudPlatformIdRelation.setPlatformId(platformId);
                        cloudPlatformIdRelation.setChannel(requestObject.getChannel());
                        cloudPlatformIdRelation.setUserType(userType);
                        cloudPlatformIdRelation.setUserId(userId);
                        cloudPlatformIdRelation.setCreateBy(requestObject.getOperationUserId());
                        cloudPlatformIdRelation.setCreationDate(LocalDateTime.now());
                        cloudPlatformIdRelation.setGmtModified(LocalDateTime.now());
                        cloudPlatformIdRelation.setModifiedBy(requestObject.getOperationUserId());
                        cloudPlatformIdRelationMapper.insert(cloudPlatformIdRelation);
                    }
                }
            }
            return cloudPaltformId;
        } catch (Throwable ex) {
            return null;
        }
    }

    /**
     * 跑批云平台同步数据
     *
     */
    public void doCloudPlatformSync() {
        String operation = "P";
        LambdaQueryWrapper<CloudPlatformSync> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CloudPlatformSync::getStatus, "0");
        List<CloudPlatformSync> lists = cloudPlatformSyncMapper.selectList(queryWrapper);
        // 获取token
        BaseReqObj baseReqObj = new BaseReqObj();
        baseReqObj.setRequestTime(new Date());
        String md5Token = cloudPlatformUtil.getMd5Token(baseReqObj);
        for (int i = 0; i < lists.size(); i++) {
            try {
                String status = "";
                String syncStatus = "0";
                CloudPlatformSync cloudPlatformSync = lists.get(i);
                if (SystemConstant.USER_TYPE_0.equals(lists.get(i).getUserType())) {
                    LambdaQueryWrapper<EnterpriseBasicInfo> queryWrapperEBI = new LambdaQueryWrapper();
                    queryWrapperEBI.eq(EnterpriseBasicInfo::getUserId, lists.get(i).getUserId());
                    if (enterpriseBasicInfoMapper.selectCount(queryWrapperEBI) > 0) {
                        EnterpriseBasicInfo enterpriseBasicInfo =
                            enterpriseBasicInfoMapper.selectList(queryWrapperEBI).get(0);
                        if (StringUtils.isNotBlank(enterpriseBasicInfo.getPapersNo())) {
                            String companyId = null;
                            CloudPlatformMerchantReq c = new CloudPlatformMerchantReq();
                            c.setCustomerType(1);
                            c.setCompanyName(enterpriseBasicInfo.getCompanyName());
                            c.setIndustryType(enterpriseBasicInfo.getInternationalIndustryClass());
                            String province = null;
                            String city = null;
                            LambdaQueryWrapper<CloudPlatformRegionRelation> queryProvince = new LambdaQueryWrapper<>();
                            queryProvince.eq(CloudPlatformRegionRelation::getUserRegionCode,
                                enterpriseBasicInfo.getOpenAccountProvince());
                            if (cloudPlatformRegionRelationMapper.selectCount(queryProvince) > 0) {
                                CloudPlatformRegionRelation cloudPlatformRegionRelation =
                                    cloudPlatformRegionRelationMapper.selectOne(queryProvince);
                                province = cloudPlatformRegionRelation.getPlatformRegionCode();
                                if (StringUtils.isNotBlank(province)) {
                                    c.setProvince(province);
                                } else {
                                    c.setProvince(cloudPlatformRegionRelation.getUserRegionName());
                                }
                            }
                            LambdaQueryWrapper<CloudPlatformRegionRelation> queryCtiy = new LambdaQueryWrapper<>();
                            queryCtiy.eq(CloudPlatformRegionRelation::getUserRegionCode,
                                enterpriseBasicInfo.getOpeningAccountCity());
                            if (cloudPlatformRegionRelationMapper.selectCount(queryCtiy) > 0) {
                                CloudPlatformRegionRelation cloudPlatformRegionRelation =
                                    cloudPlatformRegionRelationMapper.selectOne(queryCtiy);
                                city = cloudPlatformRegionRelation.getPlatformRegionCode();
                                if (StringUtils.isNotBlank(city)) {
                                    c.setCity(city);
                                } else {
                                    c.setCity(cloudPlatformRegionRelation.getUserRegionName());
                                }
                            }
                            c.setAddress(enterpriseBasicInfo.getEntRegeditAddress());
                            if (SystemConstant.PAPERS_TYPE_01.equals(enterpriseBasicInfo.getPapersType())) {
                                c.setOrganizationCode(enterpriseBasicInfo.getPapersNo());
                            } else {
                                c.setUniformSocialCreditCode(enterpriseBasicInfo.getPapersNo());
                            }
                            c.setBusinessType(enterpriseBasicInfo.getEnterpriseType());
                            c.setLegalPerson(enterpriseBasicInfo.getLegalRepresentativeName());
                            c.setLegalPersonCardNo(enterpriseBasicInfo.getLrnCertificateNo());
                            c.setBusinessState(enterpriseBasicInfo.getBusinessConditions());
                            c.setBusinessScope(enterpriseBasicInfo.getMainBusiness());
                            c.setCompanyTel(enterpriseBasicInfo.getTelephone());
                            c.setBusinessAddress(enterpriseBasicInfo.getBusinessAddress());
                            if (StringUtils.isNotBlank(enterpriseBasicInfo.getBusinessLicenseUrl())) {
                                c.setLicenseImgUrl(
                                    generalService.getOssUrl(baseReqObj, enterpriseBasicInfo.getBusinessLicenseUrl()));
                            }
                            JSONObject jsonObject = cloudPlatformUtil.getCompanyInfoSync(baseReqObj,
                                enterpriseBasicInfo.getPapersNo(), md5Token);
                            status = jsonObject.getString("status");
                            // 返回状态：0为正常， 1为异常
                            if (StringUtils.isNotEmpty(status) && "0".equals(status)) {
                                JSONObject data = jsonObject.getJSONObject("data");
                                if (data != null) {
                                    JSONObject userInfo = data.getJSONObject("companyInfo");
                                    if (userInfo != null) {
                                        companyId = userInfo.getString("companyId");
                                    }
                                }
                            }
                            if (StringUtils.isNotBlank(companyId)) {
                                cloudPlatformSync.setOperationType("1");
                            } else {
                                cloudPlatformSync.setOperationType("0");
                            }
                            baseReqObj.setChannel(cloudPlatformSync.getChannel());
                            baseReqObj.setOperationUserId(cloudPlatformSync.getCreateBy());
                            String cloudPlatformId = authenticationToCloudPlatform(baseReqObj, companyId, md5Token, c,
                                "0", enterpriseBasicInfo.getUserId(), operation);
                            if (StringUtils.isNotBlank(cloudPlatformId)) {
                                syncStatus = "1";
                            }
                        }
                    }
                } else if (SystemConstant.USER_TYPE_2.equals(lists.get(i).getUserType())) {
                    LambdaQueryWrapper<NaturalPersonBaseInformation> queryWrapperNBI = new LambdaQueryWrapper();
                    queryWrapperNBI.eq(NaturalPersonBaseInformation::getUserId, lists.get(i).getUserId());
                    if (naturalPersonBaseInformationMapper.selectCount(queryWrapperNBI) > 0) {
                        NaturalPersonBaseInformation naturalPersonBaseInformation =
                            naturalPersonBaseInformationMapper.selectList(queryWrapperNBI).get(0);
                        if (StringUtils.isNotBlank(naturalPersonBaseInformation.getPapersNo())) {
                            String companyId = null;
                            CloudPlatformMerchantReq c = new CloudPlatformMerchantReq();
                            c.setCustomerType(0);
                            c.setCompanyName(naturalPersonBaseInformation.getUserName());
                            c.setCompanyTel(naturalPersonBaseInformation.getTelephone());
                            c.setUniformSocialCreditCode(naturalPersonBaseInformation.getPapersNo());
                            JSONObject jsonObject = cloudPlatformUtil.getCompanyInfoSync(baseReqObj,
                                naturalPersonBaseInformation.getPapersNo(), md5Token);
                            status = jsonObject.getString("status");
                            // 返回状态：0为正常， 1为异常
                            if (StringUtils.isNotEmpty(status) && "0".equals(status)) {
                                JSONObject data = jsonObject.getJSONObject("data");
                                if (data != null) {
                                    JSONObject userInfo = data.getJSONObject("companyInfo");
                                    if (userInfo != null) {
                                        companyId = userInfo.getString("companyId");
                                    }
                                }
                            }
                            if (StringUtils.isNotBlank(companyId)) {
                                cloudPlatformSync.setOperationType("1");
                            } else {
                                cloudPlatformSync.setOperationType("0");
                            }
                            baseReqObj.setChannel(cloudPlatformSync.getChannel());
                            baseReqObj.setOperationUserId(cloudPlatformSync.getCreateBy());
                            String cloudPlatformId = authenticationToCloudPlatform(baseReqObj, companyId, md5Token, c,
                                "2", naturalPersonBaseInformation.getUserId(), operation);
                            if (StringUtils.isNotBlank(cloudPlatformId)) {
                                syncStatus = "1";
                            }
                        }
                    }
                } else if (SystemConstant.USER_TYPE_3.equals(lists.get(i).getUserType())) {
                    LambdaQueryWrapper<EnterpriseUserInfo> queryWrapperEUI = new LambdaQueryWrapper();
                    queryWrapperEUI.eq(EnterpriseUserInfo::getUserId, lists.get(i).getUserId());
                    queryWrapperEUI.eq(EnterpriseUserInfo::getChannel, lists.get(i).getChannel());
                    queryWrapperEUI.orderByDesc(EnterpriseUserInfo::getGmtModified);
                    if (enterpriseUserInfoMapper.selectCount(queryWrapperEUI) > 0) {
                        EnterpriseUserInfo enterpriseUserInfo =
                            enterpriseUserInfoMapper.selectList(queryWrapperEUI).get(0);
                        String userId = null;
                        String userInfoId = null;
                        String userInfo = null;
                        JSONObject jsonObject =
                            cloudPlatformUtil.getUserInfoSync(baseReqObj, enterpriseUserInfo.getTelephone(), md5Token);
                        status = jsonObject.getString("status");
                        // 返回状态：0为正常， 1为异常
                        if (StringUtils.isNotEmpty(status) && "0".equals(status)) {
                            JSONObject data = jsonObject.getJSONObject("data");
                            if (data != null) {
                                JSONObject userInfoObj = data.getJSONObject("userInfo");
                                if (userInfoObj != null) {
                                    userId = userInfoObj.getString("userId");
                                    userInfoId = userInfoObj.getString("userInfoId");
                                }
                            }
                        }
                        if (!StringUtils.isEmpty(userId) && !StringUtils.isEmpty(userInfoId)) {
                            userInfo = userId + "+" + userInfoId;
                        }
                        CloudPlatformUserReq c = null;
                        if (StringUtils.isEmpty(userInfo)) {
                            cloudPlatformSync.setOperationType("0");
                            // 在云平台不存在，先新增，然后认证
                            // 新增
                            c = new CloudPlatformUserReq();
                            c.setUserTel(enterpriseUserInfo.getTelephone());
                            c.setEmail(enterpriseUserInfo.getEmail());
                            c.setOperateFlag("0");
                            userInfo = cloudPlatformUtil.commitUser(c, md5Token, baseReqObj, operation);
                            // 新增成功后，认证
                            if (!StringUtils.isEmpty(userInfo)) {
                                // 取出userId 和 userInfoId
                                userId = userInfo.substring(0, userInfo.indexOf("+"));
                                userInfoId = userInfo.substring(userInfo.indexOf("+") + 1);
                                c = new CloudPlatformUserReq();
                                c.setUserId(userInfoId);
                                c.setUserName(enterpriseUserInfo.getUserName());
                                c.setRealName(enterpriseUserInfo.getUserName());
                                c.setUserCardNo(enterpriseUserInfo.getPapersNo());
                                c.setIsAuth(1);
                                c.setOperateFlag("1");
                                cloudPlatformUtil.commitUser(c, md5Token, baseReqObj, operation);
                            }
                        } else {
                            // 在云平台存在，修改
                            // 取出userId 和 userInfoId
                            cloudPlatformSync.setOperationType("1");
                            userId = userInfo.substring(0, userInfo.indexOf("+"));
                            userInfoId = userInfo.substring(userInfo.indexOf("+") + 1);
                            c = new CloudPlatformUserReq();
                            c.setUserId(userInfoId);
                            c.setUserName(enterpriseUserInfo.getUserName());
                            c.setRealName(enterpriseUserInfo.getUserName());
                            c.setUserCardNo(enterpriseUserInfo.getPapersNo());
                            c.setUserTel(enterpriseUserInfo.getTelephone());
                            c.setEmail(enterpriseUserInfo.getEmail());
                            c.setOperateFlag("2");
                            cloudPlatformUtil.commitUser(c, md5Token, baseReqObj, operation);
                        }
                        if (StringUtils.isNotBlank(userId)) {
                            syncStatus = "1";
                            // 将userId对应关系入库
                            LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapperCPR = new LambdaQueryWrapper<>();
                            queryWrapperCPR.eq(CloudPlatformIdRelation::getUserId, lists.get(i).getUserId());
                            queryWrapperCPR.eq(CloudPlatformIdRelation::getPlatformId, userId);
                            queryWrapperCPR.eq(CloudPlatformIdRelation::getEnterpriseId,
                                enterpriseUserInfo.getEntOperatorId());
                            queryWrapperCPR.eq(CloudPlatformIdRelation::getChannel, enterpriseUserInfo.getChannel());
                            CloudPlatformIdRelation cloudPlatformIdRelation =
                                cloudPlatformIdRelationMapper.selectOne(queryWrapperCPR);
                            if (cloudPlatformIdRelation == null) {
                                // 将userId对应关系入库
                                CloudPlatformIdRelation cr = new CloudPlatformIdRelation();
                                cr.setUserType("3");
                                cr.setChannel(enterpriseUserInfo.getChannel());
                                cr.setUserId(lists.get(i).getUserId());
                                cr.setPlatformId(userId);
                                cr.setPlatformUserInfoId(userInfoId);
                                cr.setEnterpriseId(String.valueOf(enterpriseUserInfo.getEntOperatorId()));
                                cr.setCreateBy(enterpriseUserInfo.getModifiedBy());
                                cr.setCreationDate(LocalDateTime.now());
                                cr.setModifiedBy(enterpriseUserInfo.getModifiedBy());
                                cr.setGmtModified(LocalDateTime.now());
                                cloudPlatformIdRelationMapper.insert(cr);
                            }
                        }
                    }
                }
                if (StringUtils.isNotEmpty(syncStatus)) {
                    cloudPlatformSync.setStatus(syncStatus);
                    cloudPlatformSync.setSyncTime(LocalDateTime.now());
                    LambdaQueryWrapper<CloudPlatformSync> queryWrapperCPS = new LambdaQueryWrapper<>();
                    queryWrapperCPS.eq(CloudPlatformSync::getId, cloudPlatformSync.getId());
                    cloudPlatformSyncMapper.update(cloudPlatformSync, queryWrapperCPS);
                }
            } catch (Exception e) {
                System.out.println("执行同步云平台异常" + e);
            } ;
        }
    }

    /**
     * 获取自然人和企业云平台id
     */
    public void getCloudPlatformId() {
        List<Map<String, String>> userList = userBaseInformationMapper.getUserIdListAboutCloudPlatform();
        // List<Map<String,String>> relationList = cloudPlatformIdRelationMapper.getUserIdAndCloudId();
        // Map<String,String> map1 = new HashMap<>();
        // map1.put("userId","1");
        // map1.put("platformId","123");
        // System.out.println(relationList.contains(map1));
        // 获取token
        BaseReqObj baseReqObj = new BaseReqObj();
        baseReqObj.setRequestTime(new Date());
        String md5Token = cloudPlatformUtil.getMd5Token(baseReqObj);
        for (int i = 0; i < userList.size(); i++) {
            Map<String, String> user = userList.get(i);
            Integer userId = Integer.parseInt(String.valueOf(user.get("userId")));
            String companyId = "";
            // 获取云平台id
            if (StringUtils.isNotBlank(user.get("ePapersNo"))) {
                companyId = cloudPlatformUtil.getCompanyInfoId(baseReqObj, user.get("ePapersNo"), md5Token);
            } else if (StringUtils.isNotBlank(user.get("nPapersNo"))) {
                companyId = cloudPlatformUtil.getCompanyInfoId(baseReqObj, user.get("nPapersNo"), md5Token);
            }
            if (StringUtils.isNotBlank(companyId)) {
                // CloudPlatformIdRelation relation = new CloudPlatformIdRelation();
                // relation.setUserId(Integer.valueOf(user.get("userId")));
                // relation.setPlatformId(companyId);
                // if(!relationList.contains(relation)){
                // CloudPlatformIdRelation cloudPlatformIdRelation = new CloudPlatformIdRelation();
                // cloudPlatformIdRelation.setUserType(user.get("userType"));
                // cloudPlatformIdRelation.setChannel(user.get("channel"));
                // cloudPlatformIdRelation.setUserId(Integer.valueOf(user.get("userId")));
                // cloudPlatformIdRelation.setPlatformId(companyId);
                // cloudPlatformIdRelation.setCreationDate(LocalDateTime.now());
                // cloudPlatformIdRelationMapper.insert(cloudPlatformIdRelation);
                // }
                LambdaQueryWrapper<CloudPlatformIdRelation> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(CloudPlatformIdRelation::getUserId, userId);
                queryWrapper.eq(CloudPlatformIdRelation::getPlatformId, companyId);
                if (cloudPlatformIdRelationMapper.selectCount(queryWrapper) == 0) {
                    CloudPlatformIdRelation cloudPlatformIdRelation = new CloudPlatformIdRelation();
                    cloudPlatformIdRelation.setUserType(user.get("userType"));
                    cloudPlatformIdRelation.setChannel(user.get("channel"));
                    cloudPlatformIdRelation.setUserId(userId);
                    cloudPlatformIdRelation.setPlatformId(companyId);
                    cloudPlatformIdRelation.setCreationDate(LocalDateTime.now());
                    cloudPlatformIdRelationMapper.insert(cloudPlatformIdRelation);
                }
            }
        }
    }
}
